//
//  UILabel+FontResizing.m
//  TravelGuideMdd
//
//  Created by HanFeng on 25/11/14.
//  Copyright (c) 2014 mafengwo.com. All rights reserved.
//

#import "UILabel+FontResizing.h"

@implementation UILabel (FontResizing)

- (void)resizeFontWithMaxSize:(int)maxSize minSize:(int)minSize
{
    // use font from provided label so we don't lose color, style, etc
    UIFont *font = self.font;
    NSString *text = self.text;
    // start with maxSize and keep reducing until it doesn't clip
    for(int i = maxSize; i >= minSize; i--) {
        font = [font fontWithSize:i];
        CGSize constraintSize = CGSizeMake(self.bounds.size.width, MAXFLOAT);
        CGSize labelSize = [text sizeWithFont:font constrainedToSize:constraintSize];
        
        if(labelSize.height <= self.bounds.size.height)
            break;
    }
    
    // Set the UILabel's font to the newly adjusted font.
    self.font = font;
    
}

@end
